package com.sky.controller.admin;

import com.github.pagehelper.PageInfo;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api("分类接口")
public class CategoryController {
    @Resource
    CategoryService categoryService;

    //增加分类
    @PostMapping
    @ApiOperation("添加分类")
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> getPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("前端传送页面信息 {}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.getPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过id获取分类")
    public Result<Category> getByID(@PathVariable Integer id) {
        Category c = categoryService.getById(id);
        return Result.success(c);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改category状态")
    public Result startAndStop(@PathVariable Integer status,Long id){
        categoryService.startAndStop(status,id);
        return Result.success();
    }
    @PutMapping
    @ApiOperation("编辑category")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }
    @DeleteMapping()
    @ApiOperation("编辑category")
    public Result delete(Long id){
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }


}
